package initialization

import (
	"fmt"
	"log"
	"os"
	"time"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"

	"2106a-zg5/demo/order/order_srv/global"
)

func MysqlInit() {
	Loggerssss()
	dsn := global.OrderConfig.Mysql
	fmt.Println(dsn)
	var err error

	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
		logger.Config{
			SlowThreshold:             time.Second, // Slow SQL threshold
			LogLevel:                  logger.Info, // Log level
			IgnoreRecordNotFoundError: true,        // Ignore ErrRecordNotFound error for logger
			ParameterizedQueries:      true,        // Don't include params in the SQL log
			Colorful:                  true,        // Disable color
		},
	)

	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		//sql 日志输出
		Logger: newLogger,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	global.DB.Debug()
	fmt.Println("Mysql database successfully")
}
